//
//  MonitorWindowController.m
//
//	Serial Remote for Mac - Control mac actions via serial commands
//	Copyright (C) 2010  Jeremiah McConnell
//
//	This program is free software: you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation, either version 3 of the License, or
//	(at your option) any later version.
//
//	This program is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

#import "MonitorWindowController.h"
#import "SerialPortReader.h"
#import "SerialPortSelectionData.h"


@implementation MonitorWindowController

@synthesize selectedPort;

- (void)awakeFromNib
{
	SerialPortSelectionData *portData = [SerialPortSelectionData sharedPortSlectionData];
	[self bind:@"selectedPort" toObject:portData withKeyPath:@"selectedPort" options:nil];
	
	SerialPortReader *reader = [SerialPortReader sharedSerialPortReader];
	[reader setMonitor:self];
}

- (IBAction)clearText:(id)sender
{
	[serialData setEditable:YES];
	[serialData	setString:@""];
	[serialData setEditable:NO];
	[serialData setNeedsDisplay:YES];
}

- (void)monitorData:(NSString *)data
{
	NSRange range;
	NSString *text;
	range.length = 0;
	range.location = [[serialData string] length];
	
	if ([lineBreakButton state] == NSOnState) {
		text = [data stringByReplacingOccurrencesOfString:@"\r" withString:@"\\r"];
		text = [text stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n\n"];
	} else {
		text = data;
	}
	
	[serialData setEditable:YES];
	[serialData setSelectedRange:range];
	[serialData insertText:text];
	[serialData setEditable:NO];
	[serialData setNeedsDisplay:YES];
}

@end
